/**
 * 
 */
package com.sc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SysGray;
import com.acc.softmgr.model.SysGrayExample;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISysGrayService;
import com.sc.softmgr.common.constant.DeleteStatus;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.web.DataGridObject;
import com.sc.softmgr.common.web.ResponseObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("sysGray")
public class SysGrayController {

	@Autowired
	private ISysGrayService sysGrayService;

	/**
	 * 查询列表页
	 * 
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSysGray", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SysGray> searchSysGray(SysGray sysGray,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows) throws Exception {
		DataGridObject<SysGray> responseObject = new DataGridObject<SysGray>();
		SysGrayExample example = new SysGrayExample();
		if (StringUtils.isNotBlank(sysGray.getOriginalServiceName())) {
			example.getCriteria().andOriginalServiceNameEqualTo(sysGray.getOriginalServiceName());
		}
		if (StringUtils.isNotBlank(sysGray.getGrayServiceName())) {
			example.getCriteria().andGrayServiceNameEqualTo(sysGray.getGrayServiceName());
		}

		if (page == null || page == 0) {
			page = 1;
		}
		example.getCriteria().andDeletedEqualTo("false");
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		// 如果sysGray有查询值,则需要进行查询

		List<SysGray> sysGrays = sysGrayService.selectByExample(example);
		responseObject.setTotal(page * rows);
		responseObject.setRows(sysGrays);
		return responseObject;
	}

	/**
	 * 新增灰度服务
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSysGray", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSysGray(SysGray sysGray,
			@RequestParam(value = "currentUserName", required = false) String currentUserName) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		sysGray.setCreateBy(currentUserName);
		sysGray.setCreateTime(new Date());
		sysGray.setDeleted(DeleteStatus.FALSE);
		sysGrayService.insert(sysGray);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改灰度服务
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSysGray", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSysGray(SysGray sysGray,
			@RequestParam(value = "currentUserName", required = false) String currentUserName) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		sysGray.setUpdateBy(currentUserName);
		sysGray.setUpdateTime(new Date());
		sysGrayService.updateByPrimaryKey(sysGray);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSysGray", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchSysGray(@PathVariable("id") java.lang.Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysGray sysGray = sysGrayService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(sysGray);
		return responseObject;
	}

	/**
	 * 删除灰度
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSysGray", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSysGray(@RequestParam java.lang.Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysGray sysGray = sysGrayService.selectByPrimaryKey(id);
		sysGray.setDeleted(DeleteStatus.TRUE);
		sysGrayService.updateByPrimaryKey(sysGray);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 获取灰度
	 * 
	 */
	@RequestMapping(value = "/getGrayService", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject getGrayService(@RequestParam String currenUserName, @RequestParam String originalServiceName) {
		SysGrayExample example = new SysGrayExample();
		example.getCriteria().andGrayUserNameEqualTo(currenUserName);
		example.getCriteria().andOriginalServiceNameEqualTo(originalServiceName);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysGray> list = sysGrayService.selectByExample(example);
		if (list != null && list.size() == 1) {
			return (JSONObject) JSON.toJSON(list.get(0));
		} else {
			return null;
		}
	}
}
